import { defineStore } from 'pinia'
import { getSubcount, getUerPlayList, search } from '@/utils/api'
import { useUserStore } from './user'
import { getMusicListsDetail, getAllMusicLists } from '@/utils/api'
import { GetAllDetailMusics, GetPlayLists, GetSubcount, MusicPlayList, PlayList } from '@/models/musciLists'


const KEYS = {
    'createMusicLists': 'createMusicLists',
    'userPlayLists': 'userPlayLists',
    'allMusicLists': 'allMusicLists'
}
const user = useUserStore()

export const getMusic = defineStore("getMusic", {
    state: () => {
        return {
            subcount: {} as GetSubcount, // 用户创建了多少个歌和收藏了多少歌歌
            playlist: [] as PlayList[], // 用户的歌单名字和列表
            playListsNum: -1, // 存放的是歌单有多少首歌 长度
            allMusics: [] as GetAllDetailMusics[], //歌单里面全部歌
            keyword: sessionStorage.getItem('keyword') || '',
            otherListsData: {} as MusicPlayList,//其他列表的数据
        }
    },
    actions: {
        /* 获取用户信息，歌单，收藏，mv，dj，数量*/
        async getUserSubbcount() {
            if (sessionStorage.getItem(KEYS.createMusicLists)) {
                this.subcount = (JSON.parse(sessionStorage.getItem(KEYS.createMusicLists) || '{}'))
            } else {
                const data = await getSubcount()
                // console.log('@@@@@@@' + data)
                if (data.code == 200) {
                    this.subcount = data
                    sessionStorage.setItem('createMusicLists', JSON.stringify(this.$state.subcount))
                }
            }
        },

        /* 获取用户歌单 */
        async getUserMusicLists() {
            if (sessionStorage.getItem(KEYS.userPlayLists)) {
                this.playlist = JSON.parse(sessionStorage.getItem(KEYS.userPlayLists) || '{}')
            } else {
                console.log(user.profile.userId)
                const { playlist, code } = await getUerPlayList(user.profile.userId)
                // console.log(playlist)
                if (code == 200) {
                    this.playlist = playlist
                    sessionStorage.setItem('userPlayLists', JSON.stringify(this.$state.playlist))
                }
            }
        },

        /* 获取歌单详细信息 */
        async getDetailMusicLists(id: number) {
            let { code, playlist } = await getMusicListsDetail(id)
            if (code == 200) {
                this.otherListsData = playlist
                this.playListsNum = playlist.trackIds.length
                await this.getAllMusics(id, this.playListsNum)
            }
        },

        /* 获取歌单所有的歌曲 */
        async getAllMusics(id: number, len: number) {
            // console.log('进入仓库')
            let { code, songs } = await getAllMusicLists(id, len)
            // let data = await getAllMusicLists(id, len)
            // console.log(data)
            if (code == 200) {
                // console.log('成功赋值')
                this.allMusics = songs
            }
        },
        async goToSearch(m: any) {
            let { code, result } = await search(m)
            if (code == 200) {
                this.allMusics = result.songs
                this.keyword = m
                sessionStorage.setItem('keyword', m)
            }
        }
    },
    getters: {
    }
})